#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "./项目2UI/ui.h"

static const char * getenv_default(const char * name, const char * dflt)
{
    return getenv(name) ?: dflt;
}

#if LV_USE_LINUX_FBDEV
static void lv_linux_disp_init(void)
{
    const char * device = getenv_default("LV_LINUX_FBDEV_DEVICE", "/dev/fb0");
    lv_display_t * disp = lv_linux_fbdev_create();

    lv_linux_fbdev_set_file(disp, device);
}
#elif LV_USE_LINUX_DRM
static void lv_linux_disp_init(void)
{
    const char * device = getenv_default("LV_LINUX_DRM_CARD", "/dev/dri/card0");
    lv_display_t * disp = lv_linux_drm_create();

    lv_linux_drm_set_file(disp, device, -1);
}
#elif LV_USE_SDL
static void lv_linux_disp_init(void)
{
    const int width  = atoi(getenv("LV_SDL_VIDEO_WIDTH") ?: "800");
    const int height = atoi(getenv("LV_SDL_VIDEO_HEIGHT") ?: "480");

    lv_sdl_window_create(width, height);
}
#else
#error Unsupported configuration
#endif

/**
 * Load a font with FreeType
 */

void my_chinese_font(char *text)
{
    // 1.创建中文字体
    lv_font_t *font = lv_freetype_font_create("/root/simhei.ttf",
                                              LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                              24,
                                              LV_FREETYPE_FONT_STYLE_NORMAL);

    // 2.创建中文字体样式
    static lv_style_t style;
    lv_style_init(&style);                                 // 初始化样式
    lv_style_set_text_font(&style, font);                  // 设置中文字体样式
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER); // 字体居中显示

    lv_obj_t *label = lv_label_create(lv_screen_active());
    lv_obj_add_style(label, &style, 0); // 👍给标签添加中文字体
    lv_label_set_text(label, text);
    lv_obj_center(label);
}

void lv_example_freetype_2(void)
{
    /*Create a font*/
    lv_font_t * font = lv_freetype_font_create("/home/ye/simhei.ttf",
                                               LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                               24,
                                               LV_FREETYPE_FONT_STYLE_NORMAL);

    if(!font) {
        LV_LOG_ERROR("freetype font create failed.");
        return;
    }

    /*Create style with the new font*/
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

    /*Create a label with the new style*/
    lv_obj_t * label = lv_label_create(lv_screen_active());
    lv_obj_add_style(label, &style, 0);
    lv_label_set_text(label, "我爱睡觉");
    lv_obj_center(label);
}

// 全局样式定义
// 函数前置声明

void my_piture()
{
    // 创建图像对象
    lv_obj_t * img = lv_image_create(lv_screen_active());
    // 设置图片源
    lv_image_set_src(img, "A:/home/ye/nv.jpg");
    // 设置图片居中
    lv_obj_center(img);

    // 声明图像
}

int main(void)
{
    lv_init();
    /*Linux display device init*/
    lv_linux_disp_init();

    printf("init input\n");

#if LV_USE_SDL
    // init input device
    lv_sdl_mouse_create();
    lv_sdl_keyboard_create();
    lv_sdl_mousewheel_create();
    lv_sdl_mousewheel_create();
#endif

    printf("init input  end\n");

#if LV_USE_LINUX_FBDEV
    {
        lv_indev_t * touch = lv_evdev_create(LV_INDEV_TYPE_POINTER, "/dev/input/event0");

        lv_evdev_set_calibration(touch, 0, 0, 1024, 600);
    }
#endif

    /*Create a Demo*/
    // lv_example_button_1();
    // lv_demo_widgets();
    // lv_demo_widgets_start_slideshow();
    //  lv_example_ime_pinyin_2();
    /*Handle LVGL tasks*/
    // lv_demo_music();

    // 计算缩放比例（例如缩小到 50%）
    ui_Screen3_screen_init();
    ui_init();

    while(1) {
        lv_timer_handler();
        usleep(5000);
    }
    return 0;
}
